<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="分类id" prop="cateId">
        <el-input
          v-model="queryParams.cateId"
          placeholder="请输入分类id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:work:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workList">
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="分类" align="center" prop="cateName" />
      <el-table-column label="出勤" align="center" prop="attendance" />
      <el-table-column label="时间" align="center" prop="workTime" width="90" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="学历" align="center" prop="edu" width="90" />
      <!-- <el-table-column label="是否可转正" align="center" prop="formal" /> -->
      <el-table-column label="工资" align="center" prop="salary" width="90" />
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <el-table-column label="邮箱" align="center" prop="email" width="90" />
      <el-table-column label="距今" align="center" prop="howLongAgo" width="90" />
      <el-table-column label="投递人数" align="center" prop="countClick" width="90" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-link v-if="scope.row.orderNum==100"
            type="success"
            @click="handleTop(scope.row, 1)"
          >置顶</el-link>
          <el-link v-if="scope.row.orderNum==1"
            type="danger"
            @click="handleTop(scope.row, 100)"
          >取消置顶</el-link>
          <el-button style="margin-left:10px"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:work:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:work:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改work对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :close-on-click-modal="false"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="分类" prop="cateId">
          <el-select v-model="form.cateId" placeholder="请选择分类">
            <el-option v-for="item in cataList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位描述">
          <!-- <editor v-model="form.content" :min-height="192" /> -->
          <el-input v-model="form.content" placeholder="请输入详情" type='textarea' rows="8"/>
        </el-form-item>
        <el-form-item label="出勤" prop="attendance">
          <el-input v-model="form.attendance" placeholder="请输入出勤" />
        </el-form-item>
        <el-form-item label="工作时间" prop="workTime">
          <el-input v-model="form.workTime" placeholder="请输入工作时间" />
        </el-form-item>
        <el-form-item label="工作地址" prop="workTime">
          <el-input v-model="form.address" placeholder="请输入工作地址" />
        </el-form-item>
        <el-form-item label="学历" prop="edu">
          <el-input v-model="form.edu" placeholder="请输入学历" />
        </el-form-item>
        <!-- <el-form-item label="是否可转正">
          <el-radio-group v-model="form.formal">
            <el-radio label="可转正">可转正</el-radio>
            <el-radio label="无转正">无转正</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="工资">
          <el-input v-model="form.salary" placeholder="请输入工资" />
        </el-form-item>
        <el-form-item label="联系方式" prop="email">
          <el-input v-model="form.email" placeholder="请输入联系方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWork,
  getWork,
  delWork,
  addWork,
  updateWork,
  exportWork
} from "@/api/system/work";
import ImageUpload from "@/components/ImageUpload";
import Editor from "@/components/Editor";
import { getCate } from "@/api/mall/cate";

export default {
  name: "Work",
  components: {
    ImageUpload,
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // work表格数据
      workList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        title: null,
        cateId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        cateId: [{ required: true, message: "分类不能为空", trigger: "blur" }],
        content: [
          { required: true, message: "职位描述不能为空", trigger: "blur" }
        ],
        attendance: [
          { required: true, message: "出勤不能为空", trigger: "blur" }
        ],
        formal: [{ required: true, message: "学历不能为空", trigger: "blur" }],
        salary: [{ required: true, message: "工资不能为空", trigger: "blur" }],
        // email: [
        //   {
        //     required: true,
        //     type: "email",
        //     message: "请输入正确的邮箱地址",
        //     trigger: ["blur", "change"]
        //   }
        // ]
      }
    };
  },
  created() {
    this.getList();
    this.getCateList();
  },
  methods: {
    /** 查询work列表 */
    getList() {
      this.loading = true;
      listWork(this.queryParams).then(response => {
        console.log(response);
        this.workList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getCateList() {
      getCate({ current: 1, size: 100, type: 1 }).then(res => {
        console.log(res);
        this.cataList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        pic: null,
        content: null,
        address: null,
        attendance: null,
        workTime: null,
        edu: null,
        formal: null,
        salary: null,
        createTime: null,
        cateId: null,
        email: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    handleTop(row, num){
      row.orderNum = num;
      updateWork(row).then(response => {
        this.msgSuccess("操作成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWork(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWork(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWork(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除 ?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delWork(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有work数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportWork(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
